// pages/Goods/details/index.js
var common = require("../../../../public.js");
var HttpUrl = common.HttpUrl
Page({
  /**
   * 页面的初始数据
   */
  data: {
    benefitVoucherArray: [], //省券列表
    cityCouponList: [], //省券列表
    backStatus: false,
    voucherShow: false, // 
    cityCouponShow: false,
    num: 1,
    isLoadingBusy: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log('传递过来的', options);
    this.setData({
      itemId: options.itemId,
      num: options.num,
      hasSelectPNo: options.hasSelectPNo ? options.hasSelectPNo : null,
      benefitItemFlag: options.benefitItemFlag,
      cityBenefitItemFlag: options.cityBenefitItemFlag,
      storeId:options.storeId 
    })
    wx.getStorage({
      key: 'user',
      success: function (res) {
        that.setData({
          userInfo: res.data.user,
          token: res.data.token,
          openid: res.data.openid,
        })
        console.log("本地数据为", res);
        // that.loginDeal();
        //  options.benefitItemFlag === 'true' && that.getVoucherList(); // 省券
        options.cityBenefitItemFlag === 'true' && that.getCityCoupons() // 市券
        // that.copuStoreList()
        // /order/getUserPromotionListOnDiscountPlatform
      },
      fail: function (res) {
        common.showToast('没有用户信息')
      }
    })
  },
  onHide: function () {
    if (this.data.backStatus == false) {
      this.popPage();
    }
  },
  // 店铺优惠券线上券
  copuStoreList(){
    var that = this
    var tempJson = {
      storeId: that.data.storeId,
      couponType: 1, //1线上券，2线下券
      token: that.data.token,
      userId: that.data.userInfo.id
    };
    common.wxRequest("/discountCoupon/getMyCouponForStoreByType", "post", tempJson, 'application/x-www-form-urlencoded', function (res) {
      console.log(res);
      if (res.status == 200) {
    
      } else {
 
      }
    });
  },
  getVoucherList() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    var that = this;
    var tempJson = {
      itemId: that.data.itemId,
      buyNums: that.data.num,
      token: that.data.token,
      userId: that.data.userInfo.id
    };
    common.wxRequest("/order/getUserPromotionListOnDiscountPlatform", "post", tempJson, 'application/x-www-form-urlencoded', function (res) {
      console.log(res);
      if (res.status == 200) {
        if (res.data.length) {
          console.log('有券');
          for (var i = 0; i < res.data.length; i++) {
            if (res.data[i].pNo == that.data.hasSelectPNo) {
              console.log('满足啦---', res.data[i].pNo);
              res.data[i].use = 1;
            }
          }
          that.setData({
            voucherShow: true,
            benefitVoucherArray: res.data
          }, _ => {
            wx.hideLoading();
          })
        } else {
          console.log('没有券');
          that.setData({
            voucherShow: true
          }, _ => {
            wx.hideLoading();
          })
        }
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none',
          duration: 5000
        })
      }
    });
  },
  getCityCoupons() { //获得市优惠卷
    let that = this
    let tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      itemId: that.data.itemId,
      buyNums: that.data.num,
    }
    this.setData({
      isLoadingBusy: true
    })
    common.post("/order/getUserCityDiscountCoupon", tempJson, function (res) {
      console.log('市', res)
      if (res.status == 200) {
        that.setData({
          cityCouponList: res.data,
          cityCouponShow: (res.data && res.data.length > 0) ? true : false,
        })
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoadingBusy: false
      })
    })
  },
  useClick(e) {
    console.log('立即使用');
    const no = e.currentTarget.dataset.no
    const use = e.currentTarget.dataset.use
    const amount = e.currentTarget.dataset.amount
    console.log("no为", no, "----use为", use, '----amount为', amount);
    if (use == 1) {
      console.log('要取消选中');
      // 取消选中
      for (var i = 0; i < this.data.benefitVoucherArray.length; i++) {
        this.data.benefitVoucherArray[i].use = 0;
      }
      this.data.hasSelectPNo = "";
      this.data.hasSelectPAmount = "";
      this.data.backStatus = false;
      this.popPage();
    } else { // 选中
      console.log('要选中');
      for (var i = 0; i < this.data.benefitVoucherArray.length; i++) {

        if (this.data.benefitVoucherArray[i].pNo == no) {
          console.log('满足啦---', no);
          this.data.benefitVoucherArray[i].use = 1;
        }
      }
      this.data.hasSelectPNo = no;
      this.data.hasSelectPAmount = amount;
      this.data.backStatus = true;
      this.popPage();
    }
    this.setData({
      benefitVoucherArray: this.data.benefitVoucherArray
    })

  },
  useCityCoupons(e) { // 使用市券
    console.log(e.currentTarget.dataset.item)
    let cityCouponNo = e.currentTarget.dataset.item.couponNo
    let hasSelectCityPAmount = e.currentTarget.dataset.item.discountsMoney
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; // 返会上个界面
    prevPage.setData({ // 返回携带参数给上个界面
      cityCouponNo, // 市级消费卷编码
      hasSelectPNo: this.data.hasSelectPNo,
      hasSelectCityPAmount, // 市级消费卷额度
      hasSelectPAmount: '' // 选择市级消费券清空省级消费券
    });
    // prevPage.refreshPage();
    wx.navigateBack({ // 返会上个界面
      delta: 1
    })
  },
  // 返回指定界面
  popPage: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; // 返会上个界面
    prevPage.setData({ // 返回携带参数给上个界面
      hasSelectPNo: this.data.hasSelectPNo,
      hasSelectPAmount: this.data.hasSelectPAmount,
      hasSelectCityPAmount: '', //清空市券
      cityCouponNo: '' //清空市券
    });
    // prevPage.refreshPage();
    wx.navigateBack({ // 返会上个界面
      delta: 1
    })

  },
})